/**
 * Description:一些注释<br/>
 * CreateTime:2019-04-17<br/>
 * Creater: yangpengfei@think.cn <br/>
 */
layui.use(['form','jquery','element','laydate','layer'],function(){
	let form = layui.form;
	let $ = layui.jquery;
	let jQuery = layui.jquery;
	let element = layui.element;
	let laydate = layui.laydate;
	let layer = layui.layer;
	
	$.ajax({
		url:"/library/managerList",
		type:"GET",
		dataType:"json",
		async:false,
		success:function(result){
			if(typeof(result) !== "undefined" && result != null && result.code === 0){
				let managerArr = result.data;
				const items = "";
				for(var i = 0,len = managerArr.length;i< len;i++){
					items += "<option value='"+managerArr[i].value+"'>"+managerArr[i].name+"</option>";
				}
				$("#managerInfoSelect").html(items);
				form.render("select","libraryManagerFilter");
			}else{
				layer.msg("暂无馆长职务人员",{icon:5});
			}
		},error:function(){
			layer.msg("初始化馆长信息失败",{icon:5});
		}
	});
	
	$.ajax({
		url:"/library/standloneItem",
		type:"GET",
		dataType:"json",
		success:function(result){
			if(typeof(result) !== "undefined" && result != null && result.code === 0){
				
				form.val("libraryFilter",{
					libraryId:result.data.libraryId,
					libraryName:result.data.libraryName,
					managerInfo:result.data.managerId,
					libraryTelPhone:result.data.libraryTelPhone,
					libraryAddress:result.data.libraryAddress,
					libraryEmail:result.data.libraryEmail,
					libraryFoundingTime:result.data.libraryFoundingTime,
					librarySynopsis:result.data.librarySynopsis
				});
			}
		},error:function(){
			layer.msg("初始化图书馆信息失败",{icon:5});
		}
	});
	
	laydate.render({
		elem:"#libraryFoundingTime"
	});
	
	form.verify({
		libraryName: function(value){
	      if(value.length < 4){
	        return '图书馆名称至少得4个字符哦！';
	      }
	    }
	    ,tel:function(value){
	    	if(!/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(value)){
	    		return "固定电话有误，请重填";
	    		}
	    },libraryAddr:function(value){
	    	if(value.length < 10){
	    		return "请填写正确的通讯地址";
	    	}
	    }
	    
	  });
	
	$(document).on('click','#resetFormData',function(){
		form.val("libraryFilter",{
			libraryId:"",
			libraryName:"",
			managerInfo:"",
			libraryTelPhone:"",
			libraryAddress:"",
			libraryEmail:"",
			libraryFoundingTime:"",
			librarySynopsis:""
		});
	});
	
	form.on('submit(libraryInfoSubmit)',function(data){
		$.ajax({
			url:"/library/save",
			type:"POST",
			data:data.field,
			dataType:"json",
			success:function(result){
				if(typeof(result) !== "undefined" && result != null && result.code === 0){
					layer.msg("保存图书馆信息成功",{icon:6});
				}else{
					layer.msg("保存图书馆信息失败",{icon:5});
				}
			},error:function(){
				layer.msg("保存图书馆信息失败",{icon:5});
			}
		});
		return false;
	});
});